package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.OrderInforDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderInforService;
import com.itheima.untils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderInforService.class)
@Transactional
public class OrderInforServiceImpl implements OrderInforService {
    @Autowired
    private OrderInforDao orderInforDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public Result addUser(Map map)throws Exception {
        try {
            //获得手机号
            String telephone = (String) map.get("telephone");
            //先去查询当天是否能够进行预约
            OrderSetting orderSettingList = orderSettingDao.selectByDate(DateUtils.parseString2Date((String) map.get("orderDate")));
            if (orderSettingList==null){
                //说明当天不在预约时间内
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }
            //根据身份证或者手机号去查询是否为会员,如果没有则注册一个
            Member member = orderInforDao.selectMember(telephone);
            if (member!=null){
                //判断用户是否与会员名称相同
                String name = (String) map.get("name");
                if (!member.getName().equals(name)){
                    //用户不同说明不是自己手机号
                    return new Result(false,"该手机号已经被注册");
                }
                //说明已经注册过 判断是否重复预约(根据用户id,套餐id,时间)
                List<Order> orderList = orderInforDao.selectRepetition(member.getId(),DateUtils.parseString2Date((String) map.get("orderDate")),(String)map.get("setmealId"));
                if (orderList!=null&&orderList.size()>0){
                    //说明已经预约过
                    return new Result(false,MessageConstant.HAS_ORDERED);
                }
            }else {
                //第一次注册的用户 自动给他注册会员
                member = new Member();
                member.setPhoneNumber(telephone);
                member.setName((String) map.get("name"));
                member.setSex((String) map.get("sex"));
                member.setRegTime(DateUtils.parseString2Date((String) map.get("orderDate")));
                orderInforDao.addMember(member);
            }
            //没有重复预约 判断预约人数是否够用
            int number = orderSettingList.getNumber();
            int reservations = orderSettingList.getReservations();
            if (reservations>=number){
                return new Result(false,MessageConstant.ORDER_FULL);
            }
            //没有预约满  则添加1
            orderSettingDao.updatereservations(reservations+1,DateUtils.parseString2Date((String) map.get("orderDate")));
            //添加数据到预约成功表
            Order order = new Order();
            order.setMemberId(member.getId());
            order.setOrderDate(DateUtils.parseString2Date((String) map.get("orderDate")));
            order.setOrderStatus(Order.ORDERSTATUS_NO);
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
            order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
            orderInforDao.add(order);
            return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"预约失败");
        }
    }

    @Override
    public Result findById(int id) {
        //关联查询所有数据
        try {
            Map map = orderInforDao.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @Override
    public Result findAll() {
        try {
            //查询所有预约
            List<Order> orderList = orderInforDao.findAll();
            for (Order order : orderList) {
                String sex = order.getMember().getSex();
                if (sex!=null&&!sex.equals("0")&&sex.length()>0){
                    if (sex.equals("1")){
                        order.getMember().setSex("男");
                    }else {
                        order.getMember().setSex("女");
                    }
                }
            }
            return new Result(true,"查询成功",orderList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
}
